package edu.sju.ssm.controller;




import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import edu.sju.ssm.entity.Publisher;
import edu.sju.ssm.form.PublisherForm;

@Controller
public class HelloController {
	@RequestMapping("/hello")

 public String hello(Model model) {
	 model.addAttribute("msg", "你好");
	 
	 return "hello";
	}
	
	@SuppressWarnings("unused")
	private String showPublisher(@RequestParam("name")String name,Model model) {

		model.addAttribute("msg","你想查找的名字"+name+"出版社");
		// TODO Auto-generated method stub
		return "publisher";
	}
	
	@RequestMapping("/publisher/{id}")
	public String showPublisherById(@PathVariable("id") int id, Model model) {
		String name = "清华大学出版社";
		
		model.addAttribute("msg", "ID为 " + id + " 的出版社是：" + name);
		return "publisher";
	}
	

	@RequestMapping(path="/publisher/add",method=RequestMethod.GET)
	private String showAddPublisherForm() {
		// TODO Auto-generated method stub
		return "add-publisher-form";
	}
	@RequestMapping(path="/publisher/add",method=RequestMethod.POST)
	private String addPublisher(
//			@RequestParam("name")String name,
//			@RequestParam("yearCreated")int yearCreated,
//			@RequestParam("intro")String intro,
			Publisher publisher,
			RedirectAttributes redirect) {
		
		redirect.addFlashAttribute("result","你要添加新出版社"+publisher.getName()+"何年建"
				+publisher.getYearCreated()+"简介"+publisher.getIntro());
//		 TODO Auto-generated method stub
//		return "add-publisher-result";
		redirect.addAttribute("result", "success");
		return "redirect:/publisher/add-result";
	}
	
	@RequestMapping(path="/publisher/add-result")
	private String showAddPublisherResult(@RequestParam("result")
		String result,
		@ModelAttribute ("msg")String msg,
		Model model) {
		// TODO Auto-generated method stub
		model.addAttribute("result",result);
		model.addAttribute("msg",msg);
		return "add-publisher-result";
	}
	
	@RequestMapping(path="/publisher/edit",method=RequestMethod.GET)
	public String showEditPublisherForm(Model model) {
		Publisher p =new Publisher();
		p.setId(1);
		p.setYearCreated(1980);
		p.setName("人民邮电出版社");
		p.setIntro("书籍");
		
		model.addAttribute("publisher",p);
		return "edit-publisher-form";
		
	}
	
	@RequestMapping(path="/publisher/edit",method=RequestMethod.POST)
	public String editPublisher(@Valid PublisherForm publisher,BindingResult result,
			Model model,
			RedirectAttributes redirect) {
		if(result.hasErrors()) {
			return "edit-publisher-form";
		}
		
		model.addAttribute("result","success");
		return "edit-publisher-result";
	}
	
}
